<?php
namespace Mobile\Controller;
use \Think\Log;

class CarController extends MobileController {
    /**
    * 获取车辆品牌列表
    * 
    */
    public function brand(){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        $model = M('CarBrand');
        $res = $model->select();
        foreach($res as &$row){
            if(!empty($row['logo'])){
                $row['logo'] = $this->getFullUrl(get_cover($row['logo'],'path'));
            }else{
                $row['logo'] = '';
            }
        }
        $this->succ($res);
    }
    
    /**
    * 获取车系列表
    * 
    * @param mixed $brand_id 品牌ID
    */
    public function series($brand_id){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        if(empty($brand_id)){
            $this->err('未提供品牌ID');
        }
        $model = M('CarSeries');
        $where['brand_id'] = $brand_id;
        $res = $model->field('series_id,series_name,catalog')->where($where)->select();
        $this->succ($res);
    }
    
    /**
    * 获取车型列表
    * 
    * @param mixed $series_id 车系ID
    */
    public function model($series_id){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        if(empty($series_id)){
            $this->err('未提供车系ID');
        }
        $model = M('CarModel');
        $where['series_id'] = $series_id;
        $res = $model->field('model_id,model_name,prefix_name')->where($where)->select();
        $this->succ($res);
    }
    
    /**
    * 获取保险公司列表
    * 
    */
    public function insurer(){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        $model = M('Insurer');
        $res = $model->field('insurer_id,insurer_name')->select();
        $this->succ($res);
    }
    
    /**
    * 获取车辆列表
    * 
    */
    public function index($p=1,$limit=10){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        $model = M('UserCar');
        $where['uid'] = $this->uid;
        $where['status'] = array('egt',0);
        $count = $model->where($where)->count();
        $resCar = $model->field('uid,status',true)->where($where)->page($p,$limit)->select();
        foreach($resCar as &$row){
            if(empty($row['img'])){
                $row['img'] = '';
            }else{
                $row['img'] = $this->getFullUrl(get_cover($row['img'],'path'));
            }
        }
        
        $ret = array(
            'count'=>$count,
            'data'=>$resCar,
        );
        
        $this->succ($ret);
    }
    
    
    /**
    * 添加或编辑车辆
    * 
    */
    public function edit(){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }

        $model = M('UserCar');
        $data = $model->create();
        /*
        $c_lcn_no = I('c_lcn_no');
        $brand_id = I('brand_id');
        $series_id = I('series_id');
        $model_id = I('model_id');
        $syx = I('syx');
        $jqx = I('jqx');
        $insurer_id = I('insurer_id');
        */
        $car_id = I('car_id');
        if(!empty($car_id)){
            //检测是否属于该用户
            $where['car_id'] = $car_id;
            $where['uid'] = $this->uid;
            
            $model->where($where)->save($data);
        }else{
            $data['uid'] = $this->uid;
            $car_id = $model->add($data);
        }
        unset($where);
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        
        $res = $model->where($where)->find();
        $this->succ($res);
    }
    
    /**
    * 删除车辆
    * 
    * @param mixed $car_id
    */
    public function del($car_id=''){
        if(empty($this->uid)){
            $this->err('用户未登录!');
        }
        if(empty($car_id)){
            $this->err('未指定车辆!');
        }

        $model = M('UserCar');
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        $data['status'] = -1;
        $model->where($where)->save($data);
        $this->succ('删除成功！');
    }
    
    /**
    * 上传行驶证
    * 
    */
    public function uploadImg(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }  
        
        $model = M('UserCar');
        $car_id = I('car_id');
        if(empty($car_id)){
            //如果未指定车辆ID的话新建
            unset($data);
            $data['c_lcn_no'] = '*-*(认证中)';
            $data['uid'] = $this->uid;
            $car_id = $model->add($data);
        }
        unset($where);
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        $res = $model->where($where)->find();
        if($res){
            /* 调用文件上传组件上传文件 */
            $Picture = D('Picture');
            $pic_driver = C('PICTURE_UPLOAD_DRIVER');
            $info = $Picture->upload(
                $_FILES,
                C('PICTURE_UPLOAD_CARIMG'),
                C('PICTURE_UPLOAD_DRIVER'),
                C("UPLOAD_{$pic_driver}_CONFIG")
            ); //TODO:上传到远程服务器

            /* 记录图片信息 */
            if($info){
                $headimg = 0;
                foreach($info as $file){
                    $headimg = $file['id'];
                }
                $data['img'] = $headimg;
                $model->where($where)->save($data);
                $ret = $this->getFullUrl(get_cover($headimg,'path'));
                $this->succ($ret);
            } else {
                $this->err($Picture->getError());
            }        
        }else{
            $this->err('无效的车辆ID');
        }
    }
    
    /**
    * 月份行驶停驶统计
    * 
    */
    public function monthLog($car_id='',$month=''){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }  
        
        if(empty($car_id)){
            $this->err('未指定车辆！');
        }
        
        $modelCar = M('UserCar');
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        $res = $modelCar->where($where)->find();
        if($res){
            $c_lcn_no = $res['c_lcn_no'];
            
            $ret = array();
            unset($where);
            $where['uid'] = $this->uid;
            $where['car_no'] = $c_lcn_no;
            $where['status'] = array('egt',2); //仅查找付款出单后的单子
            $modelOrder = M('Order');
            $resOrder = $modelOrder->where($where)->order('order_id desc')->find();
            if($resOrder){
                $obdid = $resOrder['obdid']; //获取绑定的OBDID
                if(!empty($obdid)){
                    //获取停驶行驶日志记录
                    if(empty($month)){
                        $month = date('Y-m-01');//当前月份
                    }
                    unset($where);
                    $startdate = date('Y-m-01',strtotime($month));
                    $enddate = date('Y-m-t',strtotime($startdate));
                    $where['obdid'] = $obdid;
                    $where['pdate'] = array('between',array($startdate,$enddate));
                    $modelLog = M('ObdDayLog');
                    $resLog = $modelLog->field('obdid,pdate,car_status')->where($where)->order('pdate')->select();
                    foreach($resLog as $row){
                        $ret[] = $row;
                    }
                }
            }
            $this->succ($ret);
        }else{
            $this->err('未找到指定车辆！');
        }
    }
    
    /**
    * 车辆日行程统计
    * 
    * @param mixed $car_id
    * @param mixed $date
    */
    public function dayLog($car_id='',$date=''){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }  
        
        if(empty($car_id)){
            $this->err('未指定车辆！');
        }
        
        $modelCar = M('UserCar');
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        $res = $modelCar->where($where)->find();
        if($res){
            $c_lcn_no = $res['c_lcn_no'];
            
            $ret = null;
            unset($where);
            $where['uid'] = $this->uid;
            $where['car_no'] = $c_lcn_no;
            $where['status'] = array('egt',2); //仅查找付款出单后的单子
            $modelOrder = M('Order');
            $resOrder = $modelOrder->where($where)->order('order_id desc')->find();
            if($resOrder){
                $modelOrderAmt = M('OrderAmtLog');
                
                //获取当天基本收益
                unset($where);
                $where['order_id'] = $resOrder['order_id'];
                $where['pdate'] = array('between',array($date,"$date 23:59:59.999999"));
                $where['isfrozen'] = 0;
                $where['ptype'] = 1;
                $resLog = $modelOrderAmt->where($where)->select();
                $arrOrder = array();
                $base_money = 0;
                foreach($resLog as $row){
                    $arrOrder[] = $row['order_id'];
                    $base_money += $row['chgval'];
                }
                //获取当天环保收益
                $frozen_total = 0;
                $frozen_money = 0;
                unset($where);
                $where['order_id'] = $resOrder['order_id'];
                $frozen_total = $modelOrder->where($where)->sum('day_frozen_amt');
                $where['pdate'] = array('between',array($date,"$date 23:59:59.999999"));
                $where['isfrozen'] = 1;
                $where['ptype'] = 1;
                $frozen_money = $modelOrderAmt->where($where)->sum('chgval');
                
                //获取当天其它收益
                $qt = 0;
                
                $ret = array(
                    'jb' => $base_money,
                    'hb' => $frozen_money,
                    'hbtotal' => $frozen_total,
                    'qt' => $qt,
                    'hj' => $base_money+$frozen_money+$qt,
                );
                
                $obdid = $resOrder['obdid']; //获取绑定的OBDID
                if(!empty($obdid)){
                    //获取停驶行驶日志记录
                    if(empty($date)){
                        $date = date('Y-m-d');//当前日期
                    }
                    unset($where);
                    $where['obdid'] = $obdid;
                    $where['pdate'] = $date;
                    $modelLog = M('ObdDayLog');
                    $resLog = $modelLog->where($where)->order('pdate')->find();
                    if($resLog) $ret = array_merge($ret,$resLog);
                }
            }
            $this->succ($ret);
        }else{
            $this->err('未找到指定车辆！');
        }
    }
    
    /**
    * 年度统计数据
    * 
    * @param mixed $car_id
    */
    public function yearLog($car_id=''){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }  
        
        if(empty($car_id)){
            $this->err('未指定车辆！');
        }
        
        $modelCar = M('UserCar');
        $where['car_id'] = $car_id;
        $where['uid'] = $this->uid;
        $res = $modelCar->where($where)->find();
        if($res){
            $c_lcn_no = $res['c_lcn_no'];
            
            $ret = null;
            unset($where);
            $where['uid'] = $this->uid;
            $where['car_no'] = $c_lcn_no;
            $where['status'] = array('egt',2); //仅查找付款出单后的单子
            $modelOrder = M('Order');
            $resOrder = $modelOrder->where($where)->order('order_id desc')->find();
            if($resOrder){
                $obdid = $resOrder['obdid']; //获取绑定的OBDID
                if(!empty($obdid)){
                    $start_date = $resOrder['start_date'];
                    $end_date = $resOrder['end_date'];
                    $modelLog = M('ObdDayLog');
                    $resWeekLog = $modelLog->query("select WEEKDAY(pdate) as wday,sum(mileage) as t_mileage,count(*) as t_days from __TABLE__ where obdid=$obdid and pdate between '$start_date' and '$end_date' and car_status=1 group by WEEKDAY(pdate)");
                    $resMonthLog = $modelLog->query("select MONTH(pdate) as mday,sum(mileage) as t_mileage,count(*) as t_days from __TABLE__ where obdid=$obdid and pdate between '$start_date' and '$end_date' and car_status=1 group by MONTH(pdate)");
                    $ret = array(
                        'week' => $resWeekLog,
                        'month' => $resMonthLog,
                    );
                }
            }
            $this->succ($ret);
        }else{
            $this->err('未找到指定车辆！');
        }
    }
}